package com.koo.framework.query;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.koo.common.exception.RRException;
import com.koo.common.utils.MyStrUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyBatisPlusUtil {
    public MyBatisPlusUtil() {
    }

    public static Map<String, Object> createMap(Object[] values, String... fields) {
        if (fields.length != values.length) {
            throw new RRException("参数数量不一致");
        } else {
            Map<String, Object> map = new HashMap();
            for (int i = 0; i < fields.length; ++i) {
                Object value = values[i];
                if (MyStrUtil.isNotEmpty(value)) {
                    map.put(fields[i], value);
                }
            }
            return map;
        }
    }

    public static Map<String, Object> createMap(Object obj, String... fields) {
        if (obj != null && fields != null) {
            Map<String, Object> map = new HashMap();

            for (int i = 0; i < fields.length; ++i) {
                String field = fields[i];
                Object value = MyStrUtil.getProperty(obj, field);
                if (MyStrUtil.isNotEmpty(value)) {
                    map.put(field, value);
                }
            }
            return map;
        } else {
            throw new RRException("参数不可为空");
        }
    }

    public static <T> Wrapper<T> createEQWrapper(Object obj, String... fields) {
        if (obj != null && fields != null) {
            QueryWrapper<T> wrapper = new QueryWrapper<>();
            for (int i = 0; i < fields.length; ++i) {
                String field = fields[i];
                Object value = MyStrUtil.getProperty(obj, field);
                if (MyStrUtil.isNotEmpty(value)) {
                    wrapper.eq(field, value);
                }
            }
            return wrapper;
        } else {
            throw new RRException("参数不可为空");
        }
    }

    public static <T> Wrapper<T> createEQWrapper(Object[] values, String... fields) {
        if (fields.length != values.length) {
            throw new RRException("参数数量不一致");
        } else {
            QueryWrapper<T> wrapper = new QueryWrapper();
            for (int i = 0; i < fields.length; ++i) {
                Object value = values[i];
                if (MyStrUtil.isNotEmpty(value)) {
                    wrapper.eq(fields[i], value);
                }
            }
            return wrapper;
        }
    }

    public static <T> Wrapper<T> filtersToWrapper(List<Filter> list) {
        return (QueryWrapper) (list == null ? new QueryWrapper() : filtersToWrapper((Filter[]) list.toArray(new Filter[list.size()])));
    }

    public static <T> Wrapper<T> filtersToWrapper(Filter... filters) {
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        if (filters != null) {
            int len = filters.length;
            for (int i = 0; i < len; ++i) {
                Filter item = filters[i];
                String value = item.getData();
                if (StringUtil.isNotEmpty(value)) {
                    String field = item.getField();
                    switch (item.getOp()) {
                        case EQ:
                            wrapper.eq(field, value);
                            break;
                        case GE:
                            wrapper.ge(field, value);
                            break;
                        case LE:
                            wrapper.le(field, value);
                            break;
                        case GT:
                            wrapper.gt(field, value);
                            break;
                        case LT:
                            wrapper.lt(field, value);
                            break;
                        case LK:
                            wrapper.like(field, value);
                            break;
                        case IN:
                            wrapper.in(field, value);
                            break;
                        case NE:
                            wrapper.ne(field, value);
                            break;
                        default:
                            wrapper.eq(field, value);
                    }
                }
            }
        }

        return wrapper;
    }
}
